package hangUpAndReply;

/**
 * 证明 suspend 方法可能造成死锁
 */
public class SuspendDemo implements Runnable {
    private Object o = new Object();

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();
        synchronized (o) {
            System.out.println(threadName + ">" + "挂起线程");
            Thread.currentThread().suspend();
        }
        System.out.println(threadName + ">" + "已经释放了资源");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new SuspendDemo(), "正常线程");
        thread1.start();
        Thread.sleep(1000);
        thread1.resume();
        //如果没有延时的sleep  resume 方法可能会快于 suspend 方法执行，结果线程被挂起后就不能释放资源造成死锁
        Thread deadThread = new Thread(new SuspendDemo(), "死锁线程");
        deadThread.start();
        deadThread.resume();
    }
}
